package com.woniuxy.yuekeuser.controller;


import com.woniuxy.yuekeuser.common.Result;
import com.woniuxy.yuekeuser.controller.Form.CampsiteForm;
import com.woniuxy.yuekeuser.info.PageInfo;
import com.woniuxy.yuekeuser.model.CampsiteActivity;
import com.woniuxy.yuekeuser.service.ICampsiteActivityService;
import com.woniuxy.yuekeuser.service.ICampsiteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Freedom
 * @since 2020-11-03
 */
@RestController
@CrossOrigin
@Slf4j
public class CampsiteActivityController {
    @Autowired
    private ICampsiteService iCampsiteService;

    @Autowired
    private ICampsiteActivityService iCampsiteActivityService;

    /**
     * 根据热度排名获取所有的
     * @return
     */
    @GetMapping("getCampsiteNameAll")
    public Result getCampsiteNameAll(){
        List<CampsiteForm> allCampsiteByHeat = iCampsiteService.getAllCampsiteByHeat();
        return Result.success(allCampsiteByHeat);
    }
    /**
     * 根据营地名查询活动(分页)
     * @return
     */
    @GetMapping("getActivityByCampsiteName")
    public Result getActivityByCampsiteName(@RequestParam(defaultValue = "1") int pageIndex, @RequestParam(defaultValue = "4") int pageSize,String campsiteName){
       log.info("名字{}",campsiteName);
        int index = (pageIndex-1)*pageSize;
        //分页查询营地活动
        List<CampsiteActivity> activitys = iCampsiteActivityService.findActivitys(campsiteName, index, pageSize);
        for(CampsiteActivity activity : activitys){
            activity.setImage(iCampsiteActivityService.findImageByActivityId(activity.getCampsiteActivityId()));
        }
        //获取总条数
        int count = iCampsiteActivityService.findActivityCount(campsiteName);
        //获取总页码
        int countPage = -1;
        if(count<pageSize){
            countPage = 1 ;
        }else{
            countPage = (count%pageSize ==0 ? count/pageSize : (count/pageSize)+1);
        }
        PageInfo pageInfo = new PageInfo();
        pageInfo.setList(activitys);
        pageInfo.setCount(count);
        pageInfo.setCountPage(countPage);
        pageInfo.setPageIndex(pageIndex);
        return  Result.success(pageInfo);
    }

}

